#include "image.h"
#include <stdio.h>
using namespace std;
using namespace cv;
Mat QImage2cvMat(QImage image)
{
    cv::Mat mat;
    switch(image.format()) {
        case QImage::Format_ARGB32:
        case QImage::Format_RGB32:
        case QImage::Format_ARGB32_Premultiplied:
            mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
            break;
        case QImage::Format_RGB888:
            mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
            cv::cvtColor(mat, mat, COLOR_BGR2RGB);
            break;
        case QImage::Format_Indexed8:
            mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
            break;
    }
    return mat;
}
QImage cvMat2QImage(const cv::Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1) {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)
        image.setColorCount(256);
        for(int i = 0; i < 256; i++) {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat
        uchar *pSrc = mat.data;
        for(int row = 0; row < mat.rows; row ++) {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3) {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    } else if(mat.type() == CV_8UC4) {
        //qDebug() << "CV_8UC4";
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image.copy();
    } else {
        //qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}

Mat threshold_split(const Mat& mat)
{
    Mat binary;
    Mat channel[3];
    int i, j;
    uchar pix1, pix2, pix3;
    binary.create(mat.rows, mat.cols, CV_8UC1);
    split(mat, channel);
    for(i = 0; i < mat.rows; i++) {
        for(j = 0; j < mat.cols; j++) {
            if(Mode) {
                pix1 = channel[0].at<uchar>(i, j);
                pix2 = channel[1].at<uchar>(i, j);
                pix3 = channel[2].at<uchar>(i, j);
            } else {
                pix1 = channel[0].at<uchar>(i, j);
                pix2 = channel[1].at<uchar>(i, j);
                pix3 = channel[2].at<uchar>(i, j);
            }
            if(High1 >= Low1) {
                if(pix1 >= Low1 && pix1 <= High1) {
                } else {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                }
            } else {
                if(pix1 > High1 && pix1 < Low1) {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                } else {
                }
            }
            if(High2 >= Low2) {
                if(pix2 >= Low2 && pix2 <= High2) {
                } else {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                }
            } else {
                if(pix2 > High2 && pix2 < Low2) {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                } else {
                }
            }
            if(High3 >= Low3) {
                if(pix3 >= Low3 && pix3 <= High3) {
                    binary.at<uchar>(i, j) = 255;
                } else {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                }
            } else {
                if(pix3 > High3 && pix3 < Low3) {
                    binary.at<uchar>(i, j) = 0;
                    continue;
                } else {
                    binary.at<uchar>(i, j) = 255;
                }
            }
        }
    }
    return binary;
}


























